//JavaScript Framework 2.0 Code
try {
	Type.registerNamespace('com.yonyou.ncmm_pac_pda.BorrowInMakeEditController');
	com.yonyou.ncmm_pac_pda.BorrowInMakeEditController = function() {
		com.yonyou.ncmm_pac_pda.BorrowInMakeEditController.initializeBase(this);
		this.initialize();
	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeEditController$initialize() {
		//you can programing by $ctx API
		//get the context data through $ctx.get()
		//set the context data through $ctx.push(json)
		//set the field of the context through $ctx.put(fieldName, fieldValue)
		//get the parameter of the context through $ctx.param(parameterName)
		//Demo Code:
		//    var str = $ctx.getString();      //获取当前Context对应的字符串
		//    alert($ctx.getString())          //alert当前Context对应的字符串
		//    var json = $ctx.getJSONObject(); //获取当前Context，返回值为json
		//    json["x"] = "a";        //为当前json增加字段
		//    json["y"] = [];           //为当前json增加数组
		//    $ctx.push(json);            //设置context，并自动调用数据绑定
		//    
		//    put方法需手动调用databind()
		//    var x = $ctx.get("x");    //获取x字段值
		//    $ctx.put("x", "b");     //设置x字段值
		//    $ctx.put("x", "b");     //设置x字段值
		//    $ctx.databind();            //调用数据绑定才能将修改的字段绑定到控件上
		//    var p1 = $param.getString("p1");   //获取参数p2的值，返回一个字符串
		//    var p2 = $param.getJSONObject("p2");   //获取参数p3的值，返回一个JSON对象
		//    var p3 = $param.getJSONArray("p3");   //获取参数p1的值，返回一个数组

		//your initialize code below...

	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeEditController$evaljs(js) {
		eval(js)
	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeEditController$locationRef(sender, args) {
		var stocrdoc = $ctx.getString("cwarehouseid");
		if(NCMMPubUtil.isEmpty(stocrdoc)) {
			$alert("仓库不能为空！");
			return;
		}
		var wheresql = " where pk_stordoc='" + stocrdoc + "'";
		NCMMPubUtil.openRefPage("PDARACK", "pk_rack_name", "pk_rack", "pk_rack", wheresql);
	}

	function onScantxtInput(sender, args) {
		$ctx.dataCollect();
		var marbarcode = $ctx.getString("vbarcode");
		parseMarCode(marbarcode);
		$id("textbox0").set("value", null);
		$id("textbox0").focus();
	}

	//解析物料码
	function parseMarCode(marbarcode) {
		var listJson = $ctx.getJSONObject();
		if(NCMMPubUtil.isEmpty(marbarcode)) {
			$alert("物料码为空！");
			return;
		}
		var marJson = NCMMBarcodeParseUtil.parseMaterialBar(marbarcode);
		var marID = marJson.pk_material;
		if(NCMMPubUtil.isEmpty(marID)) {
			$alert(marJson.failmsg);
			return;
		}
		listJson["cmaterialvid"] = marJson.pk_material;
		listJson["materialcode"] = marJson.code;
		var conditon = " where pk_material='" + marJson.pk_material + "'";
		var results = NCMMPubUtil.queryVONoCallback(MaterialVO, conditon);
		if(NCMMPubUtil.isEmpty(results) || results == "[]") {
			$alert("不存在当前物料，请更新物料档案！");
			return;
		}
		results = $stringToJSON(results);
		if(results.length == 1) {
			listJson["materialname"] = results[0].name;
			listJson["materialspec"] = results[0].materialspec;
			listJson["materialtype"] = results[0].materialtype;
			//主单位
			listJson["cunitid"] = results[0].pk_measdoc;
			listJson["cunitid_name"] = results[0].pk_measdoc_name;
			//辅单位默认取和主单位一样
			listJson["castunitid"] = results[0].pk_measdoc;
			listJson["vchangerate"] = "1/1";
			listJson["castunitid_name"] = results[0].pk_measdoc_name;
			var conditionconvert = " where pk_material='" + results[0].pk_material + "' and isstockmeasdoc='Y'";
			var converts = NCMMPubUtil.queryVONoCallback(MaterialConvertVO, conditionconvert);
			if(NCMMPubUtil.isNotEmpty(converts) && NCMMPubUtil.isNotEmpty(converts[0])) {
				var convert = $stringToJSON(converts[0]);
				//辅单位取物料档案下默认库存单位
				listJson["castunitid"] = convert.pk_measdoc;
				//和库存单位换算率
				listJson["vchangerate"] = convert.vchangerate;
				listJson["castunitid_name"] = convert.measdoc_name;
			}
		}
		if(NCMMPubUtil.isNotEmpty(marJson.vbatchcode)) {
			listJson["vbatchcode"] = marJson.vbatchcode;
		}
		listJson["vserialcode"] = marJson.vserialcode;
		var num = 1;
		var vchangerate = listJson["vchangerate"];
		var nastnum = NCMMPubUtil.getAstnum(1, vchangerate);
		if(NCMMPubUtil.isNotEmpty(marJson.nnum)) {
			num = marJson.nnum;
			nastnum = NCMMPubUtil.getAstnum(num, vchangerate);
		} else if(NCMMPubUtil.isNotEmpty(marJson.nassistnum)) {
			nastnum = marJson.nassistnum;
			num = NCMMPubUtil.getNum(nastnum, vchangerate);
		}
		var bitnum = NCMMPubUtil.getUnitBitnumber(listJson["cunitid"]);
		if(!NCMMPubUtil.isEmpty(bitnum)) {
			num = num.toFixed(bitnum);
		}
		var astbitnum = NCMMPubUtil.getUnitBitnumber(listJson["castunitid"]);
		if(!NCMMPubUtil.isEmpty(astbitnum)) {
			nastnum = nastnum.toFixed(astbitnum);
		}
		listJson["nscannum"] = num;
		listJson["nscanastnum"] = NCMMPubUtil.getAstnum(listJson["nscannum"], listJson["vchangerate"]);

		if(NCMMPubUtil.isEmpty(listJson["ctrantypeid"])) {
			$alert("借入类型不能为空！");
			return;
		}
		if(NCMMPubUtil.isEmpty(listJson["cwarehouseid"])) {
			$alert("入库仓库不能为空！");
			return;
		}
		var stockorgid;
		var stockorg = $cache.read("defaultstockorg") ? $cache.read("defaultstockorg") : "";
		if(stockorg != "") {
			stockorg = $stringToJSON(stockorg);
			stockorgid= stockorg["pk_stockorg"];
		} else {
			$alert("库存组织不能为空，请设置！");
			return;
		}
		listJson["pk_org"]=stockorgid;
		listJson["pk_scan"] = getBorrowInMakeScanPK();
		NCMMPubUtil.insertVO(BorrowInMakeVO, listJson);
		$ctx.push(listJson);
	}
	function com$yonyou$ncmm_pac_pda$BorrowInMakeEditController$onload(sender, args) {
		var row = $param.getJSONObject("row");
		NCMMPubUtil.createTable(BorrowInMakeVO);
		if(!(row == null || row == "")) {
			var json = {};
			json = row;
			json = NCMMPubUtil.htmlspecialjson2UI(json);
			$ctx.push(json);
			$ctx.dataBind();
		} else {
			var stordoc = $cache.read("defaultstordoc") ? $cache.read("defaultstordoc") : "";
			if(stordoc != "") {
				stordoc = $stringToJSON(stordoc);
				$ctx.put("cwarehouseid", NCMMPubUtil.htmlspecialchars2UI(stordoc["pk_stordoc"]));
				$ctx.put("cwarehouseid_name", NCMMPubUtil.htmlspecialchars2UI(stordoc["name"]));
				$ctx.dataBind();
			}
			var billtype = $cache.read("default49billtype") ? $cache.read("default49billtype") : "";
			if(billtype != "") {
				billtype = $stringToJSON(billtype);
				$ctx.put("ctrantypeid", NCMMPubUtil.htmlspecialchars2UI(billtype["pk_billtypeid"]));
				$ctx.put("ctrantypeid_name", NCMMPubUtil.htmlspecialchars2UI(billtype["name"]));
				$ctx.dataBind();
			}
		}
	}

	function back(sender, args) {
		$view.close();
	}

	function loadmenu(sender, args) {
		BorrowInMakeUtil.openDropDownListForScan("btn_menu");
	}

	function localSave(sender, args) {
		var json = $ctx.getJSONObject();
		if(json.nscannum == null || json.nscannum == 0 || json.nscannum == "0") {
			$alert("主数量不能为空或0！");
			return;
		}
		if(json.nscanastnum == null || json.nscanastnum == 0 || json.nscanastnum == "0") {
			$alert("数量不能为空或0！");
			return;
		}
		if(!$.isNumeric(json.nscannum) || !$.isNumeric(json.nscanastnum)) {
			$alert("数量输入不合法！");
			return;
		}
		var stockorgid;
		var stockorg = $cache.read("defaultstockorg") ? $cache.read("defaultstockorg") : "";
			if(stockorg != "") {
				stockorg = $stringToJSON(stockorg);
				 stockorgid= stockorg["pk_stockorg"];
			} else {
				$alert("库存组织不能为空，请设置！");
				return;
		}
		json["pk_org"]=stockorgid;
		json = NCMMPubUtil.htmlspecialjson2UI(json);
		var isSuccess = doSave(json);
		if(isSuccess == true) {
			$toast("保存成功！");
		}
	}

	function doSave(json) {		
	   
		if(NCMMPubUtil.isEmpty(json["ctrantypeid"])) {
			$alert("入库类型不能为空！");
			return;
		}
		if(NCMMPubUtil.isEmpty(json["cwarehouseid"])) {
			$alert("入库仓库不能为空！");
			return;
		}
		if(NCMMPubUtil.isEmpty(json["pk_scan"])) {
			json["pk_scan"] = getBorrowInMakeScanPK();
			console.log("json = " + $jsonToString(json));
			NCMMPubUtil.insertVO(BorrowInMakeVO, json);
		} else {
			console.log("json = " + $jsonToString(json));
			NCMMPubUtil.updateVO(BorrowInMakeVO, json);
		}
		$ctx.push(json);
		$ctx.dataBind();
		return true;
	}

	function getBorrowInMakeScanPK() {
		var date = new Date().getTime();
		var pk = "4008060449" + date.toString();
		return pk;
	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeEditController$imagebutton0_onclick(sender, args) {
		$scanner.open({
			bindfield: "vbarcode",
			callback: "onScantxtInput()"
		});
	}

	function marScanBack() {
		var marbarcode = $ctx.getString("vbarcode");
		if(marbarcode != null && marbarcode != "") {
			// 扫描的是物料码
			$id("textbox0").set("value", marbarcode);
			$ctx.dataBind();
		}
		parseMarCode(marbarcode);
		$id("textbox0").set("value", null);
	}

	//提交
	function onOneScanSubmit(sender, args) {
		var json = $ctx.getJSONObject();
		if(NCMMPubUtil.isEmpty(json)) {
			$alert("无扫描数据，不能提交！");
			return;
		}
		var stockorgid;
		var stockorg = $cache.read("defaultstockorg") ? $cache.read("defaultstockorg") : "";
			if(stockorg != "") {
				stockorg = $stringToJSON(stockorg);
				 stockorgid= stockorg["pk_stockorg"];
			} else {
				$alert("库存组织不能为空，请设置！");
				return;
			}
		if(json.nscannum == null || json.nscannum == 0 || json.nscannum == "0") {
			$alert("主数量不能为空或0！");
			return;
		}
		if(json.nscanastnum == null || json.nscanastnum == 0 || json.nscanastnum == "0") {
			$alert("数量不能为空或0！");
			return;
		}
		if(!$.isNumeric(json.nscannum) || !$.isNumeric(json.nscanastnum)) {
			$alert("数量输入不合法！");
			return;
		}
		json = NCMMPubUtil.htmlspecialjson2UI(json);
		var isSuccess = doSave(json);
		if(isSuccess != true) {
			$js.hideLoadingBar();
			return;
		}
		var sql = " where pk_scan='" + json.pk_scan + "'";
		var vo = null;
		NCMMPubUtil.queryVO(BorrowInMakeVO, sql, function(scanData) {
			if(typeof(scanData) == 'undefined' || scanData == null || scanData.length == 0) {
				return;
			}
			var vos = eval(scanData);
			vos = NCMMPubUtil.htmlspecialjsonarray2UI(vos);
			vo = vos[0];
		});
		if(NCMMPubUtil.isEmpty(vo)) {
			$js.hideLoadingBar();
			$alert("当前扫描记录已经被删除，不能提交！")
			return;
		}
		var head = {};
		head["pk_org"] = stockorgid;
		head["cwarehouseid"] = vo["cwarehouseid"];
		head["ctrantypeid"] = vo["ctrantypeid"];
		var item = {};
		item["cmaterialvid"] = vo["cmaterialvid"];
		item["nscanastnum"] = vo["nscanastnum"];
		item["nscannum"] = vo["nscannum"];
		item["castunitid"] = vo["castunitid"];
		item["cunitid"] = vo["cunitid"];
		item["vchangerate"] = vo["vchangerate"];
		item["vbatchcode"] = vo["vbatchcode"];
		//$alert($jsonToString(vo));
		if(NCMMPubUtil.isNotEmpty(vo["vserialcode"]) || NCMMPubUtil.isNotEmpty(vo["pk_rack"])) {
			var bbitems = [];
			var bbitem = {};
			bbitem["vserialcode"] = vo["vserialcode"];
			bbitem["clocationid"] = vo["pk_rack"];
			bbitem["nnum"] = (vo["nscannum"] == "" ? "1.00000" : vo["nscannum"]);
			bbitems.push({
				"bbitem": bbitem
			});
			item["bbitems"] = bbitems;
		}
		var billinfo = {};
		billinfo["head"] = head;
		var items = [{
			"item": item
		}];
		billinfo["items"] = items;
		var billinfos = [{
			"billinfo": billinfo
		}];
		//$alert($jsonToString(billinfos));
		BorrowInMakeUtil.commitData(billinfos,[vo["pk_scan"]],function(data){		 
		   var list = {  "pk_org":vo["pk_org"],       
		   "ctrantypeid": vo["ctrantypeid"],
								"ctrantypeid_name": vo["ctrantypeid_name"],
								"cwarehouseid": vo["cwarehouseid"],
								"cwarehouseid_name": vo["cwarehouseid_name"],
								"pk_rack": vo["pk_rack"],
								"pk_rack_name": vo["pk_rack_name"],
							};
							$ctx.push(list);	 
							$ctx.dataBind(); 		 
		});		
	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeEditController$oninputNum(sender, args) {
		$ctx.dataCollect();
		var num = $ctx.getString("nscannum");
		if(num == null || num == "" || num == "-") {
			$ctx.put("nscanastnum", num);
			$ctx.dataBind();
			return;
		}
		if(!NCMMPubUtil.isNumber(num)) {
			//此时第一次输入
			var astnum = $ctx.getString("nscanastnum");
			if(astnum == null || astnum == "") {
				$ctx.put("nscannum", "");
			} else {
				$ctx.put("nscannum", args["oldvalue"]);
			}
			$ctx.dataBind();
			return;
		}
		if(NCMMPubUtil.isBsBitnumber(num, $ctx.getString("cunitid"))) {
			$ctx.put("nscannum", args["oldvalue"]);
			$ctx.dataBind();
			return;
		}
		var vchangerate = $ctx.getString("vchangerate");
		var astnum = NCMMPubUtil.getAstnum(num, vchangerate);
		var bitnum = NCMMPubUtil.getUnitBitnumber($ctx.getString("castunitid"));
			if(!NCMMPubUtil.isEmpty(bitnum)) {
				astnum = NCMMPubUtil.decimalPrecision(astnum,bitnum);
			}
			if(astnum.toString() == "NaN") {
				$alert("扫描主数量输入不合法！");
				return;
			}
		$ctx.put("nscanastnum", astnum);
		$ctx.dataBind();

	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeEditController$inputAstNum(sender, args) {
		$ctx.dataCollect();
		var astnum = $ctx.getString("nscanastnum");
		if(astnum == null || astnum == "" || astnum == "-") {
			$ctx.put("nscannum", astnum);
			$ctx.dataBind();
			return;
		}
		if(!NCMMPubUtil.isNumber(astnum)) {
			//此时第一次输入
			var num = $ctx.getString("nscannum");
			if(num == null || num == "") {
				$ctx.put("nscanastnum", "");
				$ctx.put("nscannum", "");
				$ctx.dataBind();
			} else {
				$ctx.put("nscanastnum", args["oldvalue"]);
				$ctx.dataBind();
			}
			return;
		}
		if(NCMMPubUtil.isBsBitnumber(astnum, $ctx.getString("castunitid"))) {
			$ctx.put("nscanastnum", args["oldvalue"]);
			$ctx.dataBind();
			return;
		}
		var vchangerate = $ctx.getString("vchangerate");
		var num = NCMMPubUtil.getNum(astnum, vchangerate);
		var bitnum = NCMMPubUtil.getUnitBitnumber($ctx.getString("cunitid"));
			if(!NCMMPubUtil.isEmpty(bitnum)) {
				num = NCMMPubUtil.decimalPrecision(num,bitnum);
			}
			if(num.toString() == "NaN") {
				$alert("数量输入不合法！");
				return;
			}
		$ctx.put("nscannum", num);
		$ctx.dataBind();

	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeEditController$cspaceScan(sender, args) {
		$scanner.open({
			bindfield: "rack",
			callback: "rackScanBack()"
		});
	}

	function rackScanBack() {
		var stocrdoc = $ctx.getString("cwarehouseid");
		if(NCMMPubUtil.isEmpty(stocrdoc)) {
			$alert("仓库不能为空！");
			$ctx.put("pk_rack_name", "");
			$ctx.put("pk_rack", "");
			$ctx.dataBind();
			return;
		}
		var rack = $ctx.getString("rack");
		var wheresql = " where code='" + rack + "'";
		if(!NCMMPubUtil.isEmpty(stocrdoc)) {
			wheresql = wheresql + " and pk_stordoc='" + stocrdoc + "'";
		}
		getRackInfoByWhereSql(wheresql, rack);
	}

	function getRackInfoByWhereSql(wheresql, rack) {
		if(NCMMPubUtil.isTableExist(RackVO) == false) {
			$alert("没有扫描货位【" + rack + "】记录，请更新货位数据，或检查当前扫描货位是否属于当前仓库！");
			$ctx.put("pk_rack_name", "");
			$ctx.put("pk_rack", "");
			$ctx.dataBind();
			return;
		}
		NCMMPubUtil.queryVO(RackVO, wheresql, function(data) {
			//2 打开扫描记录编辑界面
			if(NCMMPubUtil.isEmpty(data)) {
				$alert("没有扫描货位【" + rack + "】记录，请更新货位数据，或检查当前扫描货位是否属于默认仓库！");
				$ctx.put("pk_rack_name", "");
				$ctx.put("pk_rack", "");
				$ctx.dataBind();
				return;
			}
			var vos = eval(data);
			$ctx.put("pk_rack_name", vos[0].name);
			$ctx.put("pk_rack", vos[0].pk_rack);
			$ctx.dataBind();
		});
	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeEditController$billtypeRef(sender, args) {
		NCMMPubUtil.openBilltypeRefPage("ctrantypeid", "ctrantypeid_name", "49");
	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeEditController$astUnitRef(sender, args) {
		var listJson = $ctx.getJSONObject();
		var mateiralID = listJson.cmaterialvid;
		if(NCMMPubUtil.isEmpty(mateiralID)) {
			$alert("请先扫描物料！");
			return;
		}
		NCMMPubUtil.openAstUnitRefPage("castunitid_name", "castunitid", "vchangerate", mateiralID,"nscannum","nscanastnum");

	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeEditController$stockRef(sender, args) {
		var stockorg = $cache.read("defaultstockorg");
		if(stockorg == null || stockorg == "") {
			$alert("默认库存组织不能为空，请设置！");
			return;
		}
		var wheresql = "";
		NCMMPubUtil.openRefPage("PDASTORDOC", "cwarehouseid_name", "cwarehouseid", "pk_stordoc", wheresql);
		$ctx.put("pk_rack_name", "");
		$ctx.put("pk_rack", "");
		$ctx.dataBind();
	}

	function rackEdit(sender, args) {
		var stocrdoc = $ctx.getString("cwarehouseid");
		if(NCMMPubUtil.isEmpty(stocrdoc)) {
			$alert("仓库不能为空！");
			$ctx.put("pk_rack_name", "");
			$ctx.put("pk_rack", "");
			$ctx.dataBind();
			return;
		}
		var rack = $ctx.getString("pk_rack_name");
		var wheresql = " where (code='" + rack + "' or name ='" + rack + "')";
		if(!NCMMPubUtil.isEmpty(stocrdoc)) {
			wheresql = wheresql + " and pk_stordoc='" + stocrdoc + "'";
		}
		getRackInfoByWhereSql(wheresql, rack);

	}
	com.yonyou.ncmm_pac_pda.BorrowInMakeEditController.prototype = {
		stockRef: com$yonyou$ncmm_pac_pda$BorrowInMakeEditController$stockRef,
		astUnitRef: com$yonyou$ncmm_pac_pda$BorrowInMakeEditController$astUnitRef,
		billtypeRef: com$yonyou$ncmm_pac_pda$BorrowInMakeEditController$billtypeRef,
		cspaceScan: com$yonyou$ncmm_pac_pda$BorrowInMakeEditController$cspaceScan,
		inputAstNum: com$yonyou$ncmm_pac_pda$BorrowInMakeEditController$inputAstNum,
		oninputNum: com$yonyou$ncmm_pac_pda$BorrowInMakeEditController$oninputNum,
		locationRef: com$yonyou$ncmm_pac_pda$BorrowInMakeEditController$locationRef,
		imagebutton0_onclick: com$yonyou$ncmm_pac_pda$BorrowInMakeEditController$imagebutton0_onclick,
		onload: com$yonyou$ncmm_pac_pda$BorrowInMakeEditController$onload,
		initialize: com$yonyou$ncmm_pac_pda$BorrowInMakeEditController$initialize,
		evaljs: com$yonyou$ncmm_pac_pda$BorrowInMakeEditController$evaljs
	};
	com.yonyou.ncmm_pac_pda.BorrowInMakeEditController.registerClass('com.yonyou.ncmm_pac_pda.BorrowInMakeEditController', UMP.UI.Mvc.Controller);
} catch(e) {
	$e(e);
}